package com.example.qihuibao.widget.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.bean.*
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.extension.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/13
 *     desc   :
 */
class EmployInfoLayout(ctx:Context) : FrameLayout(ctx) {
    private lateinit var activity: MainActivity
    private lateinit var root: View
    private var modify = false
    private val employList = mutableListOf<RspQueryEmployInfoBean.Employ>()
    private lateinit var rv_employ: CommonRecyclerView

    init {
        if (ctx is MainActivity) {
            activity = ctx
        }
        root = LayoutInflater.from(ctx).inflate(R.layout.layout_employ, this, true)
        root.setOnClickListener {  }//禁止点击事件穿透
        initView()
    }

    private fun initView() {
        root.findViewById<TextView>(R.id.tv_title).text = "人员信息"
        root.findViewById<View>(R.id.iv_back).setOnClickListener { (parent as ViewGroup).removeView(this) }
        root.findViewById<View>(R.id.tv_update).setOnClickListener { update() }
        root.findViewById<View>(R.id.tv_add).setOnClickListener { add() }
        root.findViewById<View>(R.id.tv_delete).setOnClickListener { delete() }
        queryEmployInfo(1, mutableListOf())
    }

    private fun queryEmployInfo(pageNo: Int, list: MutableList<RspQueryEmployInfoBean.Employ>) {
        activity?.getMainViewModel().queryEmployInfo(ReqQueryEmployInfoBean(PageNo = pageNo)).observe(activity) {
            if (it.RetCode == "0") {
                if (!it.EmpList.isNullOrEmpty()) {
                    list.addAll(it.EmpList!!)
                }
                if (it.HasNext == "1") {
                    queryEmployInfo(pageNo + 1, list)
                } else {
                    employList.clear()
                    employList.addAll(list)
                    //show data
                    rv_employ = root.findViewById<CommonRecyclerView>(R.id.rv_employ)
                    rv_employ.init(
                        R.layout.item_text,
                        LinearLayoutManager(context, RecyclerView.VERTICAL, false),
                        employList,
                        { parent, pos ->
                            if (employList.isNotEmpty()) {
                                (parent as TextView).text = "员工姓名：${employList[pos].EmpName}\n" +
                                        "员工类型：${employList[pos].EmpType}\n" +
                                        "一级部门：${employList[pos].DepID1}\n" +
                                        "状态：${employList[pos].Status}"
                            }
                        },
                        {pos ->
                            showDetail(employList[pos])
                        }
                    )
                }
            }else {
                activity?.toast("查询失败")
            }
        }
    }

    private fun showDetail(employ:RspQueryEmployInfoBean.Employ) {
        root.findViewById<EdittextWithText>(R.id.et_EmpID).setContent(employ.EmpID)
        root.findViewById<EdittextWithText>(R.id.et_EmpName).setContent(employ.EmpName)
        root.findViewById<EdittextWithText>(R.id.et_EmpType).setContent(employ.EmpType)
        root.findViewById<EdittextWithText>(R.id.et_DepID1).setContent(employ.DepID1)
        root.findViewById<EdittextWithText>(R.id.et_Status).setContent("${employ.Status}")
    }

    private fun update() {
        val EmpID = root.findViewById<EdittextWithText>(R.id.et_EmpID).getContent()
        val EmpName = root.findViewById<EdittextWithText>(R.id.et_EmpName).getContent()
        val EmpType = root.findViewById<EdittextWithText>(R.id.et_EmpType).getContent()
        val DepID1 = root.findViewById<EdittextWithText>(R.id.et_DepID1).getContent()
        val Status = root.findViewById<EdittextWithText>(R.id.et_Status).getContent()
        val Balance = root.findViewById<EdittextWithText>(R.id.et_Balance).getContent()
        val Balance1 = root.findViewById<EdittextWithText>(R.id.et_Balance1).getContent()
        val Balance2 = root.findViewById<EdittextWithText>(R.id.et_Balance2).getContent()
        if (EmpID.isEmpty() || EmpName.isEmpty() || EmpType.isEmpty() || DepID1.isEmpty() || Status.isEmpty()) {
            activity.toast("输入不能为空")
            return
        }
        val req = ReqUpdateEmployInfoBean(
            EmpID=EmpID,
            EmpName=EmpName,
            EmpType=EmpType,
            DepID1=DepID1,
            Status=Status,
            DepID2=root.findViewById<EdittextWithText>(R.id.et_DepID2).getContent(),
            ReserveBindMsg=root.findViewById<EdittextWithText>(R.id.et_ReserveBindMsg).getContent(),
            Balance= if (Balance.isEmpty()) 0 else Balance.toInt(),
            Balance1= if (Balance1.isEmpty()) 0 else Balance1.toInt(),
            Balance2= if (Balance2.isEmpty()) 0 else Balance2.toInt(),
            DefCantID=root.findViewById<EdittextWithText>(R.id.et_DefCantID).getContent(),
            ICCardNo=root.findViewById<EdittextWithText>(R.id.et_ICCardNo).getContent(),
            Duty=root.findViewById<EdittextWithText>(R.id.et_Duty).getContent(),
            Sex=root.findViewById<EdittextWithText>(R.id.et_Sex).getContent(),
            TeleNum=root.findViewById<EdittextWithText>(R.id.et_TeleNum).getContent(),
            MobiNum=root.findViewById<EdittextWithText>(R.id.et_MobiNum).getContent(),
            EntryDate=root.findViewById<EdittextWithText>(R.id.et_EntryDate).getContent(),
            LeaveDate=root.findViewById<EdittextWithText>(R.id.et_LeaveDate).getContent(),
        )
        activity.getMainViewModel().updateEmployInfo(req).observe(activity) {
            activity.toast("${if (it.RetCode == "0") "更新成功" else "更新失败：${it.RetMsg}"}")
            if (it.RetCode == "0") {
                queryEmployInfo(1, mutableListOf())
            }
        }
    }

    private fun add() {
        val EmpID = root.findViewById<EdittextWithText>(R.id.et_EmpID).getContent()
        val EmpName = root.findViewById<EdittextWithText>(R.id.et_EmpName).getContent()
        val EmpType = root.findViewById<EdittextWithText>(R.id.et_EmpType).getContent()
        val DepID1 = root.findViewById<EdittextWithText>(R.id.et_DepID1).getContent()
        val Status = root.findViewById<EdittextWithText>(R.id.et_Status).getContent()
        val Balance = root.findViewById<EdittextWithText>(R.id.et_Balance).getContent()
        val Balance1 = root.findViewById<EdittextWithText>(R.id.et_Balance1).getContent()
        val Balance2 = root.findViewById<EdittextWithText>(R.id.et_Balance2).getContent()
        if (EmpID.isEmpty() || EmpName.isEmpty() || EmpType.isEmpty() || DepID1.isEmpty() || Status.isEmpty()) {
            activity.toast("输入不能为空")
            return
        }
        val req = ReqAddEmployInfoBean(
            EmpID=EmpID,
            EmpName=EmpName,
            EmpType=EmpType,
            DepID1=DepID1,
            Status=Status,
            CertType=root.findViewById<EdittextWithText>(R.id.et_CertType).getContent(),
            CertCode=root.findViewById<EdittextWithText>(R.id.et_CertCode).getContent(),
            DepID2=root.findViewById<EdittextWithText>(R.id.et_DepID2).getContent(),
            ReserveBindMsg=root.findViewById<EdittextWithText>(R.id.et_ReserveBindMsg).getContent(),
            Balance= if (Balance.isEmpty()) 0 else Balance.toInt(),
            Balance1= if (Balance1.isEmpty()) 0 else Balance1.toInt(),
            Balance2= if (Balance2.isEmpty()) 0 else Balance2.toInt(),
            DefCantID=root.findViewById<EdittextWithText>(R.id.et_DefCantID).getContent(),
            ICCardNo=root.findViewById<EdittextWithText>(R.id.et_ICCardNo).getContent(),
            Duty=root.findViewById<EdittextWithText>(R.id.et_Duty).getContent(),
            Sex=root.findViewById<EdittextWithText>(R.id.et_Sex).getContent(),
            TeleNum=root.findViewById<EdittextWithText>(R.id.et_TeleNum).getContent(),
            MobiNum=root.findViewById<EdittextWithText>(R.id.et_MobiNum).getContent(),
            EntryDate=root.findViewById<EdittextWithText>(R.id.et_EntryDate).getContent(),
            LeaveDate=root.findViewById<EdittextWithText>(R.id.et_LeaveDate).getContent()
        )
        activity.getMainViewModel().addEmployInfo(req).observe(activity) {
            activity.toast("${if (it.RetCode == "0") "新增成功" else "新增失败：${it.RetMsg}"}")
            if (it.RetCode == "0") {
                queryEmployInfo(1, mutableListOf())
            }
        }
    }

    private fun delete() {
        val EmpID = root.findViewById<EdittextWithText>(R.id.et_EmpID).getContent()
        val EmpName = root.findViewById<EdittextWithText>(R.id.et_EmpName).getContent()
        if (EmpID.isEmpty() || EmpName.isEmpty()) {
            activity.toast("输入不能为空")
            return
        }
        val req = ReqDeleteEmployInfoBean(EmpID=EmpID, EmpName=EmpName)
        activity.getMainViewModel().deleteEmployInfo(req).observe(activity) {
            activity.toast("${if (it.RetCode == "0") "删除成功" else "删除失败：${it.RetMsg}"}")
            if (it.RetCode == "0") {
                queryEmployInfo(1, mutableListOf())
            }
        }
    }
}